package com.platform.server.controller;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.paltform.server.entity.SysUser;
import com.platform.server.dao.WithdrawDao;
import com.platform.server.service.SkillService;

@Controller
public class SkillController {

	@Autowired
	private SkillService skillService;	
	
	@Autowired
	private WithdrawDao withdrawDao;
	
	/**
	 * 技能列表
	 */
	@RequestMapping("/skillList")
	public Object skillList(HttpServletRequest request,String id,String startTime,String endTime,String serverName,String content){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<Map<String, Object>> lists = skillService.skillList(id,startTime,endTime,serverName,content);
		for(int i = 0;i < lists.size();i++){
			lists.get(i).put("create_time",sdf.format(lists.get(i).get("create_time")));
		}
		request.setAttribute("lists",lists);
		request.setAttribute("nums",lists.size());
		if(id != null){
			return "/ui/server/skill_info.jsp";
		}
		return "/ui/server/skill_list.jsp";
	}
	
	/**
	 * 添加分类
	 * @throws JSONException 
	 */
	@RequestMapping("/insertServer")
	public void insertServer(HttpSession session,String server_name,String server_sub_name,String server_price,String detailed,HttpServletResponse response) throws Exception{
		int flag = skillService.insertServer(server_name, server_sub_name, server_price, detailed);
		SysUser su = (SysUser) session.getAttribute("user");
		String user = null;
		if(su != null){
			user = su.getSys_name();
		}
		withdrawDao.insertLog(user, "添加了服务分类");
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("flag", flag);
		response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
	}
	
	/**
	 * 删除服务
	 */
	@RequestMapping("/deleteServer")
	public Object deleteServer(String idd){
		int flag = skillService.deleteServer(idd);
		return "/skillList";
	}
	
	/**
	 * 修改服务
	 */
	@RequestMapping("/updateServer")
	public void updateServer(HttpSession session,String id, String server_sub_name, String server_name, String server_price,String detailed,HttpServletResponse response) throws Exception{
		int flag = skillService.updateServer(id, server_sub_name, server_name, server_price, detailed);
		SysUser su = (SysUser) session.getAttribute("user");
		String user = null;
		if(su != null){
			user = su.getSys_name();
		}
		withdrawDao.insertLog(user, "修改了服务分类");
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("flag", flag);
		response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
	}
}
